﻿using System;
using fastCSharp;
using fastCSharp.code.cSharp;

namespace diantou.web.onlineJudge
{
    /// <summary>
    /// 用户OJ主页
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsReferer = false)]
    partial class user : web.user.view<user>
    {
        /// <summary>
        /// 问题信息
        /// </summary>
        private struct problem
        {
            /// <summary>
            /// 问题标识
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public int Id;
            /// <summary>
            /// AC问题百分比
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public int AcceptedPercentage;
            /// <summary>
            /// 标题
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public string Title;
            /// <summary>
            /// 是否金牌
            /// </summary>
            public bool IsGold;
            /// <summary>
            /// 是否银牌
            /// </summary>
            public bool IsSilver;
            /// <summary>
            /// 是否铜牌
            /// </summary>
            public bool IsBronze;
            /// <summary>
            /// 问题URL
            /// </summary>
            [fastCSharp.code.ignore]
            public diantou.dataModel.path.problem Path
            {
                get { return new diantou.dataModel.path.problem { Id = Id }; }
            }
            /// <summary>
            /// 是否AC问题
            /// </summary>
            [fastCSharp.code.ignore]
            public bool IsAccepted;
            /// <summary>
            /// 用户标识
            /// </summary>
            [fastCSharp.code.ignore]
            public int UserId;
            /// <summary>
            /// 提交记录地址
            /// </summary>
            [fastCSharp.code.ignore]
            public webView.hashUrl UserSubmitPath
            {
                get
                {
                    return new diantou.dataModel.path.user { Id = UserId }.OnlineJudgeProblemSubmit(Id);
                }
            }
        }
        /// <summary>
        /// 分组问题
        /// </summary>
        [fastCSharp.code.cSharp.webView.clientType(Name = "diantou.groupProblem", MemberName = null)]
        private sealed class group
        {
            /// <summary>
            /// 问题集合
            /// </summary>
            public list<problem> Problems;
            /// <summary>
            /// 问题集合1
            /// </summary>
            public list<problem> LeftProblems
            {
                get
                {
                    int count = Problems.count();
                    return new list<problem>(Problems.UnsafeArray, 0, (Problems.count() + 1) >> 1, true);
                }
            }
            /// <summary>
            /// 问题集合2
            /// </summary>
            public list<problem> RightProblems
            {
                get
                {
                    int count = Problems.count(), index = (count + 1) >> 1;
                    return new list<problem>(Problems.UnsafeArray, index, count - index, true);
                }
            }
            /// <summary>
            /// 问题分组
            /// </summary>
            public diantou.dataAccess.problemGroup Group;
            /// <summary>
            /// AC问题数量
            /// </summary>
            public int AcceptedCount;
            /// <summary>
            /// 分数
            /// </summary>
            public int Point;
            /// <summary>
            /// AC问题分数
            /// </summary>
            public int AcceptedPoint;
            /// <summary>
            /// 是否显示标题
            /// </summary>
            [fastCSharp.code.ignore]
            public bool IsTitle = true;
            /// <summary>
            /// 是否显示问题
            /// </summary>
            [fastCSharp.code.ignore]
            public bool IsHide;
            /// <summary>
            /// 分组问题
            /// </summary>
            /// <param name="group"></param>
            public group(diantou.dataAccess.problemGroup group)
            {
                Group = group;
                Problems = new list<problem>();
            }
            /// <summary>
            /// 添加问题
            /// </summary>
            /// <param name="problem"></param>
            /// <returns>AC百分比</returns>
            public int Add(diantou.dataAccess.problem.userTitle problem)
            {
                int acceptedPercentage;
                if (problem.TestDataCount == short.MinValue) acceptedPercentage = -1;
                else if (problem.ErrorCount == 0)
                {
                    if (problem.TestDataCount == 0) acceptedPercentage = 0;
                    else
                    {
                        acceptedPercentage = 100;
                        ++AcceptedCount;
                        AcceptedPoint += problem.Point;
                    }
                }
                else acceptedPercentage = (problem.TestDataCount - problem.ErrorCount) * 100 / problem.TestDataCount;
                Point += problem.Point;
                Problems.Add(new problem { Id = problem.ProblemId, Title = problem.Title, AcceptedPercentage = acceptedPercentage, IsGold = problem.IsGold, IsSilver = problem.IsSilver, IsBronze = problem.IsBronze });
                return acceptedPercentage;
            }
        }
        /// <summary>
        /// 问题分组集合
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        private group[] groups;
        /// <summary>
        /// 分组菜单
        /// </summary>
        private groupMenu groupMenu
        {
            get { return new groupMenu { ViewOnly = requestHeader.IsReView }; }
        }

        /// <summary>
        /// 用户OJ主页
        /// </summary>
        /// <param name="userId">用户标识</param>
        /// <returns>是否成功</returns>
        private new bool loadView(int userId)
        {
            if (base.load(userId, diantou.dataModel.path.pub.Default.OnlineJudgeAllProblemPath))
            {
                diantou.dataAccess.problem.userTitle[] problems = diantou.dataAccess.tcpCall.problem.getUserTitles(userId);
                diantou.dataAccess.problemGroup[] problemGroups;
                if (diantou.dataAccess.clientCache.ProblemGroup.IsData) problemGroups = diantou.dataAccess.clientCache.ProblemGroup.CacheValues.getArray();
                else problemGroups = diantou.dataAccess.tcpCall.problemGroup.getCache();
                group[] groupArray = new group[problemGroups.maxKey(value => value.Id, 0) + 1];
                group group0 = new group(diantou.dataAccess.problemGroup.NullGroup);
                groupArray[0] = group0;
                foreach (diantou.dataAccess.problemGroup group in problemGroups) groupArray[group.Id] = new group(group);
                foreach (diantou.dataAccess.problem.userTitle problem in problems)
                {
                    group group = groupArray[problem.GroupId];
                    int acceptedPercentage = group.Add(problem);
                    if (problem.TestDataCount == 0 || problem.ErrorCount != 0)
                    {
                        group0.Problems.Add(new problem { Id = problem.ProblemId, Title = problem.Title, AcceptedPercentage = acceptedPercentage, UserId = userId });
                        group0.Point += problem.Point;
                    }
                }
                int index = 0;
                if (group0.Problems.Count == 0) groupArray[0] = null;
                int count = index;
                while (++index < groupArray.Length)
                {
                    if (groupArray[index] != null) groupArray[count++] = groupArray[index];
                }
                Array.Resize(ref groupArray, count);
                groups = groupArray;
                return true;
            }
            return false;
        }
    }
}
